﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Toom.CellularAutomata.Model.Fast
{
	public abstract class CARuleStaticFast
	{
		protected CAFieldFast field;
		public abstract int CalculateTransform(CAPointFast p);
	}


	public class CARuleStaticFastSumEightNeighborsConway : CARuleStaticFast
	{
		public CARuleStaticFastSumEightNeighborsConway(CAFieldFast f)
		{
			this.field = f;
		}

		public override int CalculateTransform(CAPointFast p)
		{
			int newVal;
			int sum = 0;

			for (int j = -1; j <= 1; j++)
				for (int i = -1; i <= 1; i++)
				{
					if (i == 0 && j == 0) continue;

					CAVectorFast v = new CAVectorFast(i, j);
					CAPointFast p2 = CAPointFast.AddVector(field,p, v);
					int d = field.GetDataAt(p2);
					if (d == 1)
						sum++;
				}

			newVal = 0;
			int curVal = field.GetDataAt(p);

			if (curVal == 0)
			{
				if (sum == 3)
					newVal = 1;
			}
			else if (curVal == 1)
			{
				if (sum == 3 || sum == 2)
					newVal = 1;
			}
			else
				//throw new Exception("data is not 0 or 1.... hmmmmm.....");
				newVal = int.MinValue;

			return newVal;
		}
	}//class RuleSumEightNeighborsConway



}//ns
